﻿using System.Collections.Generic;
using UnityEditor;
using UnityEditor.AddressableAssets.Build;
using UnityEditor.AddressableAssets.Settings;
using UnityEngine;

public class AnalyzeRuleLua : AnalyzeRuleAddressEqualPath
{
    public override bool CanFix
    {
        get { return true;}
        set { }
    }

    public override string ruleName
    {
        get { return "AnalyzeRuleLua"; }
    }


    [SerializeField]
    List<AddressableAssetEntry> m_MislabelEntries = new List<AddressableAssetEntry>();

    protected override void OnRefreshAnalysis(AddressableAssetGroup group, AddressableAssetEntry e, List<AnalyzeResult> results)
    {
        base.OnRefreshAnalysis(group, e, results);

        if (!e.labels.Contains(AddressablesConfig.LuaLabel))
        {
            m_MislabelEntries.Add(e);
            results.Add(new AnalyzeResult { resultName = group.Name + kDelimiter + e.address + " miss label=" + AddressablesConfig.LuaLabel, severity = MessageType.Warning });
        }
    }

    protected override string OnGetGroupName()
    {
        return AddressablesConfig.LuaGroup;
    }


    public override void FixIssues(AddressableAssetSettings settings)
    {
        base.FixIssues(settings);
        foreach (var e in m_MislabelEntries)
        {
            e.SetLabel(AddressablesConfig.LuaLabel, true);
        }
        m_MislabelEntries = new List<AddressableAssetEntry>();
    }

    public override void ClearAnalysis()
    {
        base.ClearAnalysis();
        m_MislabelEntries = new List<AddressableAssetEntry>();
    }
}


[InitializeOnLoad]
class RegisterAnalyzeRuleLua
{
    static RegisterAnalyzeRuleLua()
    {
        AnalyzeSystem.RegisterNewRule<AnalyzeRuleLua>();
    }
}
